import { cancelOrder } from "@src/api/booking";

Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    order: {
      type: Object
    },
    refundRule: {
      type: Object
    }
  },
  data: {
    reasonList: ["行程变更", "日期选错", "人数变更，需要调整", "价格较贵，通过其他渠道预订好了", "选择了其它店家"],
    reason: "行程变更",
    otherReason: "",
    qxRuleFree: 0,
    reject: null,
    resolve: null,
    qxRule: [],
    dialogShow: false,
  },
  observers: {
    'order,refundRule': function (order, refundRule) {
      if (!order || !refundRule) {
        return;
      }
      this.parseRule(order, refundRule)
    }
  },
  methods: {
    parseRule(order, refundRule) {
      let qxRule = [], qxRuleFree = 0;
      if (order.subStatus >= 2) {
        if (refundRule.cancelRule === 0) {
          qxRule.push("本次取消免费");
          qxRuleFree = 0;
          qxRule.push(`订单确认前可免费取消`);
        } else if (refundRule.cancelRule === 1) {
          qxRuleFree = 0;
          qxRule.push(`本次取消免费`);
          qxRule.push(`当天${refundRule.cancelLatestFreeTime}前可免费取消，其他时间不可取消`);
        } else if (refundRule.cancelRule === 2) {
          let {totalFee,discountFee,pointsDiscountFee,supermemberPointsDiscountFee} = order
          let subValue = (refundRule.cancelChargeRatio / 100 * (totalFee - discountFee - pointsDiscountFee-supermemberPointsDiscountFee)/ 100).toString();
          let subFind = subValue.indexOf('.')
          if (subFind > -1) {
            qxRuleFree = subValue.substring(0, subFind + 3)
          } else {
            qxRuleFree = subValue
          }
          qxRule.push(`本次取消需要扣除取消费用`);
          qxRule.push(`取消订单需扣除实付金额的${qxRuleFree}元)`)
        } else if (refundRule.cancelRule === 3) {
          qxRuleFree = 0;
          qxRule.push(`本次取消免费`);
          qxRule.push(`到店时间前${(refundRule.cancelLatestFreeMinute / 60).toFixed(1)}小时前可免费取消，其他时间不可取消`);
        }
      } else {
        qxRule.push("本次取消免费");
        qxRuleFree = 0;
        qxRule.push("订单确认前可免费取消");
      }
      this.setData({ qxRule, qxRuleFree })
    },
    open() {
      return new Promise(((resolve, reject) => {
        this.setData({ resolve, reject, dialogShow: true });
      }))
    },
    handleStop() {
      return true;
    },
    handleSelectReason(e) {
      let reason = e.currentTarget.dataset.reason;
      this.setData({ reason: reason })
    },
    handleConfirm() {
      let reason = this.data.reason;
      let { order, order: { bizData } } = this.data
      let { endDay } = JSON.parse(bizData)
      if (reason && this.data.otherReason) {
        reason = reason + ":" + this.data.otherReason
      }
      cancelOrder({ orderNo: this.data.order.orderNo, reason: this.data.reason }).then(res => {
        this.data.resolve && this.data.resolve()
        this.setData({ dialogShow: false });
      }).catch(() => {
        this.data.resolve && this.data.resolve()
        this.setData({ dialogShow: false });
      });
    },
    handleClose() {
      this.data.resolve && this.data.resolve()
      this.setData({ dialogShow: false });
    }
  }
});
